using STD.Shared.Domain.Entities;
using STD.Shared.Domain.ValueObjects;
using STD.Shared.Domain.Events;

namespace STD.Services.UserService.Domain.Entities;

/// <summary>
/// 用户实体
/// </summary>
public class User : BaseEntity
{
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; private set; }
    
    /// <summary>
    /// 邮箱
    /// </summary>
    public Email Email { get; private set; }
    
    /// <summary>
    /// 密码哈希
    /// </summary>
    public string PasswordHash { get; private set; }
    
    /// <summary>
    /// 姓名
    /// </summary>
    public string FullName { get; private set; }
    
    /// <summary>
    /// 手机号
    /// </summary>
    public string? PhoneNumber { get; private set; }
    
    /// <summary>
    /// 是否激活
    /// </summary>
    public bool IsActive { get; private set; }
    
    /// <summary>
    /// 最后登录时间
    /// </summary>
    public DateTime? LastLoginAt { get; private set; }
    
    private User() { }
    
    /// <summary>
    /// 创建用户
    /// </summary>
    /// <param name="username">用户名</param>
    /// <param name="email">邮箱</param>
    /// <param name="passwordHash">密码哈希</param>
    /// <param name="fullName">姓名</param>
    /// <param name="phoneNumber">手机号</param>
    /// <returns>用户实例</returns>
    public static User Create(string username, string email, string passwordHash, string fullName, string? phoneNumber = null)
    {
        var user = new User
        {
            Username = username,
            Email = Email.Create(email),
            PasswordHash = passwordHash,
            FullName = fullName,
            PhoneNumber = phoneNumber,
            IsActive = true
        };
        
        return user;
    }
    
    /// <summary>
    /// 更新用户信息
    /// </summary>
    /// <param name="fullName">姓名</param>
    /// <param name="phoneNumber">手机号</param>
    public void UpdateProfile(string fullName, string? phoneNumber)
    {
        FullName = fullName;
        PhoneNumber = phoneNumber;
        UpdatedAt = DateTime.UtcNow;
    }
    
    /// <summary>
    /// 更新邮箱
    /// </summary>
    /// <param name="email">新邮箱</param>
    public void UpdateEmail(string email)
    {
        Email = Email.Create(email);
        UpdatedAt = DateTime.UtcNow;
    }
    
    /// <summary>
    /// 更新密码
    /// </summary>
    /// <param name="passwordHash">新密码哈希</param>
    public void UpdatePassword(string passwordHash)
    {
        PasswordHash = passwordHash;
        UpdatedAt = DateTime.UtcNow;
    }
    
    /// <summary>
    /// 激活用户
    /// </summary>
    public void Activate()
    {
        IsActive = true;
        UpdatedAt = DateTime.UtcNow;
    }
    
    /// <summary>
    /// 停用用户
    /// </summary>
    public void Deactivate()
    {
        IsActive = false;
        UpdatedAt = DateTime.UtcNow;
    }
    
    /// <summary>
    /// 记录登录
    /// </summary>
    public void RecordLogin()
    {
        LastLoginAt = DateTime.UtcNow;
        UpdatedAt = DateTime.UtcNow;
    }
    
    /// <summary>
    /// 删除用户
    /// </summary>
    public void Delete()
    {
        IsDeleted = true;
        UpdatedAt = DateTime.UtcNow;
    }
} 